package com.learner.course.service.impl;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.course.mapper.CoursePlanMapper;
import com.learner.course.repository.CourseBaseRepository;
import com.learner.course.repository.CoursePlanRepository;
import com.learner.course.service.CoursePlanService;
import com.learner.pojo.course.CourseBase;
import com.learner.pojo.course.CoursePlan;
import com.learner.pojo.course.expand.CoursePlanNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author ice2020x
 * @Description: 课程计划的业务处理
 */
@Service
@Slf4j
public class CoursePlanServiceImpl implements CoursePlanService {

    @Autowired
    CoursePlanMapper coursePlanMapper;

    @Autowired
    CoursePlanRepository coursePlanRepository;

    @Autowired
    CourseBaseRepository courseBaseRepository;


    @Override
    public CoursePlan getCoursePlanIdByCourseId(String courseId) {
        log.info("根据courseId获取coursePlanId,课程id为:{}", courseId);
        List<CoursePlan> coursePlans = coursePlanRepository.findByCourseIdAndParentId(courseId, "0");
        return coursePlans.get(0);
    }

    @Override
    public CoursePlanNode findCoursePlanListByCourseId(String courseId) {
        return coursePlanMapper.selectListByCourseId(courseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CoursePlan addCoursePlan(CoursePlan coursePlan) {
        if (coursePlan == null || StringUtils.isEmpty(coursePlan.getCourseId()) || StringUtils.isEmpty(coursePlan.getPname())) {
            throw ExceptionUtil.createException(RestException.class, 1000002);
        }
        System.out.println(coursePlan);

        // 课程ID
        String courseId = coursePlan.getCourseId();
        String parentId = coursePlan.getParentId();

        // 分情况讨论 pname
        if (StringUtils.isEmpty(parentId)) {
            // 根据课程id 查询课程名
            Optional<CourseBase> optional = courseBaseRepository.findById(courseId);
            if (!optional.isPresent()) {
                // 为空说明课程不存在
                throw ExceptionUtil.createException(RestException.class, 2000003);
            } else {
                // 不为空说名课程存在
                CourseBase o = (CourseBase) optional.get();
                List<CoursePlan> courseIdAndParentId = coursePlanRepository.findByCourseIdAndParentId(courseId, "0");
                if (courseIdAndParentId == null || courseIdAndParentId.size() <= 0) {
                    System.out.println("dads");
                    CoursePlan coursePlanTemp = new CoursePlan();
                    coursePlanTemp.setParentId("0");
                    coursePlanTemp.setGrade("1");
                    coursePlanTemp.setPname(o.getName());
                    coursePlanTemp.setCourseId(courseId);
                    coursePlanTemp.setStatus("0");
                    coursePlanRepository.save(coursePlanTemp);
                    parentId = coursePlanTemp.getId();
                } else {
                    parentId = courseIdAndParentId.get(0).getId();
                }
            }

        }

        log.info("父节点id:{}", parentId);
        Optional<CoursePlan> optional = coursePlanRepository.findById(parentId);
        CoursePlan coursePlan0 = optional.get();
        // 父结点的级别
        String parentGrade = coursePlan0.getGrade();
        // 创建一个新结点准备添加
        CoursePlan coursePlanTemp = new CoursePlan();
        // 将plan的属性拷贝到planNew中
        BeanUtils.copyProperties(coursePlan, coursePlanTemp);
        // 要设置必要的属性
        coursePlanTemp.setParentId(parentId);
        if ("1".equals(parentGrade)) {
            coursePlanTemp.setGrade("2");
        } else {
            coursePlanTemp.setGrade("3");
        }
        coursePlanTemp.setStatus("0");
        return coursePlanRepository.save(coursePlanTemp);
    }

}
